package com.example.patients;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import com.example.patients.database.DbPatients;
import com.example.patients.entity.PatientsBean;

import java.util.ArrayList;
import java.util.List;

public class AddPatientsActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton BtnBack;//返回按钮
    private TextView tv_title;//头标题
    private EditText EtName, EtNumber, EtYear, EtSex, EtSickness, EtTaboo, EtPhone;
    //姓名、病床号、年龄、性别、病因、禁忌史、电话
    private Button BtnSave;//保存按钮
    DbPatients dbPatients;//数据库
    List<PatientsBean> mData;//数据源
    String number;//病床号

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_patients);
        tv_title = findViewById(R.id.tv_title);//绑定控件
        BtnBack = findViewById(R.id.btn_back);
        BtnSave = findViewById(R.id.btn_save);
        BtnBack.setOnClickListener(this);
        BtnSave.setOnClickListener(this);
        EtNumber = findViewById(R.id.add_et_number);
        EtName = findViewById(R.id.add_et_name);
        EtYear = findViewById(R.id.add_et_year);
        EtSex = findViewById(R.id.add_et_sex);
        EtSickness = findViewById(R.id.add_et_sickness);
        EtTaboo = findViewById(R.id.add_et_taboo);
        EtPhone = findViewById(R.id.add_et_phone);
        dbPatients = new DbPatients(this);
        mData = new ArrayList<>();
        initData();
    }

    /**
     * 保存修改的信息
     */
    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            number = intent.getStringExtra("number");
            if (number != null) {
                tv_title.setText("修改病人信息");
                BtnSave.setText("修  改");
                EtNumber.setText(intent.getStringExtra("number"));
                EtName.setText(intent.getStringExtra("name"));
                EtYear.setText(intent.getStringExtra("year"));
                EtSex.setText(intent.getStringExtra("sex"));
                EtSickness.setText(intent.getStringExtra("sickness"));
                EtTaboo.setText(intent.getStringExtra("taboo"));
                EtPhone.setText(intent.getStringExtra("phone"));
            }
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_back) {
            Intent intent1 = new Intent(AddPatientsActivity.this, MainActivity.class);
            startActivity(intent1);
        } else if (id == R.id.btn_save) {
            //输入
            String number = EtNumber.getText().toString().trim();
            String name = EtName.getText().toString().trim();
            String year = EtYear.getText().toString().trim();
            String sex = EtSex.getText().toString().trim();
            String sickness = EtSickness.getText().toString().trim();
            String taboo = EtTaboo.getText().toString().trim();
            String phone = EtPhone.getText().toString().trim();
            PatientsBean patientsBeans = dbPatients.QueryFromPatientByNumber(number);
            if (patientsBeans != null) {//修改操作
                if (name.length() > 0) {
                    showDeleteDialog1(number, name, year, sex, sickness,taboo,phone);
//                    if (dbPatients.updatePatient(number, name, year, sex, sickness, taboo, phone)) {
//                        Toast.makeText(AddPatientsActivity.this, "修改成功", Toast.LENGTH_LONG).show();
//                        setResult(2);
//                        showDialog2();
//                    }
                } else {
                    Toast.makeText(AddPatientsActivity.this, "修改的内容不能为空！", Toast.LENGTH_LONG).show();
                }
            } else if (TextUtils.isEmpty(name)) {
                Toast.makeText(AddPatientsActivity.this, "录入失败！请输入姓名", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(number)) {
                Toast.makeText(AddPatientsActivity.this, "录入失败！请输入病床号", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(year)) {
                Toast.makeText(AddPatientsActivity.this, "录入失败！请输入年龄", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(sex)) {
                Toast.makeText(AddPatientsActivity.this, "录入失败！请输入性别", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(sickness)) {
                Toast.makeText(AddPatientsActivity.this, "录入失败！请输入病因", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(taboo)) {
                Toast.makeText(AddPatientsActivity.this, "录入失败！请输入禁忌史", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(phone)) {
                Toast.makeText(AddPatientsActivity.this, "录入失败！请输入电话", Toast.LENGTH_SHORT).show();
            } else {
                System.out.println("是否为空: " + dbPatients);
                dbPatients.insertPatients(number, name, year, sex, sickness, taboo, phone);
                dbPatients.updateTotalPatientCount(dbPatients.getTotalPatientCount() + 1);//更新总人数
                showDialog();
            }
        }
    }

    /**
     * 保存提示框
     */
    private void showDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("录入成功提示")
                .setMessage("已成功录入！\n是否继续？")
                .setPositiveButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(AddPatientsActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //将输入框清空
                        EtNumber.setText("");
                        EtName.setText("");
                        EtYear.setText("");
                        EtSex.setText("");
                        EtSickness.setText("");
                        EtTaboo.setText("");
                        EtPhone.setText("");
                    }
                });
        builder.create().show();
    }

    /**
     * 覆盖原数据提示框
     */
    private void showDeleteDialog1(String number,String name, String year,String sex,String sickness,String taboo,String phone) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("修改当前病人信息提示")
                .setMessage("确定修改该病床病人信息？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (dbPatients.updatePatient(number, name, year, sex, sickness, taboo, phone)) {
                            Toast.makeText(AddPatientsActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                            setResult(2);
                            showDialog2();
                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(AddPatientsActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
        builder.create().show();
    }

    /**
     * 修改提示框
     */
    private void showDialog2() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("修改成功提示")
                .setMessage("已成功修改！")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(AddPatientsActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
        builder.create().show();
    }
}
